package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;

import util.JDBCUtil;
import domain.Actor;
import domain.User;
import ex.DatabaseException;

public class ActorDAO {
	private Connection conn;
	private PreparedStatement stmt;
	private ResultSet rs;
	
	private static final String CREATE_ACTOR="insert into actorPysFea (height,weight,hairColor,eyeColor,skinColor,uid) values (?,?, ?, ?, ?, ?) ";
	private static final String EDIT_ACTOR="update actorPysFea set height = ?, weight = ?, hairColor = ?, eyeColor = ?, skinColor = ? where uid = ?";
	private static final String SELECT_ACTOR_BY_UID="select * from actorPysFea where uid = ?";
	private static final String DELETE_ACTOR="delete from actorPysFea where uid = ?";
	
	private static final String ALL_ACTORS = "SELECT * FROM actorPysFea"; 
	//public 
	
	public LinkedList<User> searchActor(int heightMin, int heightMax, int weightMin, int weightMax, String hairColor,
			String eyeColor, String skinColor) throws DatabaseException
	{
		ArrayList<Actor> allActors = new ArrayList<Actor>();
		LinkedList<User> result = new LinkedList<User>();
		
		try {
			conn = JDBCUtil.dequeueConn();
			stmt = conn.prepareStatement(ALL_ACTORS);
			
			rs = stmt.executeQuery();
			while (rs.next()) {
				long uid = rs.getLong("uid");
				String hColor = rs.getString("hairColor");
				String eColor = rs.getString("eyeColor");
				String sColor = rs.getString("skinColor");
				int height = rs.getInt("height");
				int weight = rs.getInt("weight");
				
				allActors.add(new Actor(uid, hColor, eColor, sColor,height, weight));
			}
			JDBCUtil.enqueuConn(conn);
			stmt.close();
			
			for (int i = 0; i < allActors.size(); i++)
			{
				Actor actor = allActors.get(i);
				if (actor.getHeight() < heightMin || actor.getHeight() > heightMax || actor.getWeight() < weightMin || actor.getWeight() > weightMax)
					continue;
				if (hairColor != null && hairColor.equals(actor.getHairColor()))
					continue;
				if (eyeColor != null && eyeColor.equals(actor.getEyeColor()))
					continue;
				if (skinColor != null && skinColor.equals(actor.getSkinColor()))
					continue;
				UserDAO uDao = new UserDAO();
				result.add(uDao.selectUserByUid(actor.getUid()));
			}
		} 
		catch (SQLException e) {
			throw new DatabaseException("Problem with retrieving user information! "	+ e.getMessage());
		}
		
		
		
		
		return result;
	}
	
	
	//Selection of the Actor Information using the User ID in the Actor table
	public Actor selectActorByUid(long uid) throws DatabaseException
	{		
		Actor actor = null;
		try {
			conn = JDBCUtil.dequeueConn();
			stmt = conn.prepareStatement(SELECT_ACTOR_BY_UID);
			stmt.setLong(1, uid);
			
			rs = stmt.executeQuery();
			if (rs.next()) {
				String hairColor = rs.getString("hairColor");
				String eyeColor = rs.getString("eyeColor");
				String skinColor = rs.getString("skinColor");
				int height = rs.getInt("height");
				int weight = rs.getInt("weight");
				
				actor =new Actor(uid, hairColor, eyeColor, skinColor,height, weight );
			}
			JDBCUtil.enqueuConn(conn);
			stmt.close();
		} 
		catch (SQLException e) {
			throw new DatabaseException("Problem with retrieving user information! "	+ e.getMessage());
		}
		return actor;
	}
	
	//Insertion of a new Actor to the database
	public void insertActor(Actor actor) throws DatabaseException {
		
		
		try {
			conn = JDBCUtil.dequeueConn();
			stmt = conn.prepareStatement(CREATE_ACTOR);
			stmt.setInt(1, actor.getHeight());
			stmt.setInt(2, actor.getWeight());
			stmt.setString(3, actor.getHairColor());
			stmt.setString(4, actor.getEyeColor());
			stmt.setString(5, actor.getSkinColor());			
			stmt.setLong(6, actor.getUid());
			
			stmt.execute();	
			JDBCUtil.enqueuConn(conn);
			stmt.close();
		} 
		catch (SQLException e) {
			throw new DatabaseException("Problem with inserting user information! "	+ e.getMessage());
		}
	}
	
	//Editing the Actor information
	public void editActor(Actor actor) throws DatabaseException {
		
		
		try {
			conn = JDBCUtil.dequeueConn();
			stmt = conn.prepareStatement(EDIT_ACTOR);
			stmt.setInt(1, actor.getHeight());
			stmt.setInt(2, actor.getWeight());
			stmt.setString(3, actor.getHairColor());
			stmt.setString(4, actor.getEyeColor());
			stmt.setString(5, actor.getSkinColor());			
			stmt.setLong(6, actor.getUid());

			stmt.execute();
			JDBCUtil.enqueuConn(conn);
			stmt.close();
		} 
		catch (SQLException e) {
			throw new DatabaseException("Problem with editing user information! "	+ e.getMessage());
		}
	}
	
	public void deleteActor(Actor actor) throws DatabaseException {
		
		
		try {
			conn = JDBCUtil.dequeueConn();
			stmt = conn.prepareStatement(DELETE_ACTOR);
			stmt.setLong(1, actor.getUid());
			stmt.execute();
			JDBCUtil.enqueuConn(conn);
			stmt.close();
		} 
		catch (SQLException e) {
			throw new DatabaseException("Problem with deleting actor! "	+ e.getMessage());
		}
	}
}
